package copy;

import java.io.*;
import java.util.Scanner;

public class CopyFile {

    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
       while(true) {
           System.out.println("请输入要复制文件的路径");
           File f1 = new File(new Scanner(System.in).next());
           if (!f1.exists()) {
               System.out.println("您输入的文件路径不存在，请重新输入");
               continue;
           }
           if (f1.isDirectory()) {
               System.out.println("您输入的是文件夹路径，请正确输入文件路径");
               continue;
           }

           System.out.println("输入成功，接下来请输入要复制的目标路径");
           File f2 = null;
           while (true) {
               f2 = new File(new Scanner(System.in).next());
               if (!f2.exists()) {
                   System.out.println("您输入的目标路径不存在，请重新输入");
                   continue;
               }
               if (f2.isDirectory()) {
                   System.out.println("您输入的是文件夹路径，请正确输入文件路径");
                   continue;
               }
               break;
           }

           fis = new FileInputStream(f1);
           fos = new FileOutputStream(f2);
           int len = 0;
           byte[] bytes = new byte[1024];
           while ((len = fis.read(bytes)) != -1) {
               fos.write(bytes, 0, len);
           }
           System.out.println("复制成功");
           break;
       }
            fis.close();
            fos.close();

    }


}
